package org.example.session;

import org.example.config.Configuration;
import org.example.xml.XMLConfigBuilder;

import java.io.IOException;
import java.io.InputStream;

public class SqlSessionFactoryBuilder {

    /**
     * 1.解析mybatis配置文件
     * 2.构建DefaultSqlSessionFactory
     */
    public SqlSessionFactory build(InputStream inputStream) {
        //XML配置构建器:通过构造器加载资源文件
        XMLConfigBuilder parser = new XMLConfigBuilder(inputStream);
        //执行解析
        Configuration configuration = parser.parse(inputStream);
        return new DefaultSqlSessionFactory(configuration);
    }

    public SqlSessionFactory build(String config) {
        try {
            return build(Resources.getResourceAsStream(config));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
